package com.etoak.controller;

import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.entity.Territory;
import com.etoak.service.ITerritoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Checklist库领域表controller
 *
 * @author zhixuebin
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/goertek/checklist/territory")
public class TerritoryController extends BaseController {

    @Autowired
    ITerritoryService territoryService;

    /**
     * 添加领域
     * @param territory
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Territory territory){
        //验重
        List<Territory> search = territoryService.getTerritory(territory);
        int i = 0;
        if(StringUtils.isNull(search) || search.size() < 1){
            //设置主键
            String id = IdUtils.simpleUUID();
            territory.setId(id);
            i = territoryService.add(territory);
        }else{
            return AjaxResult.error("领域已存在！");
        }
        return AjaxResult.success("添加成功");
    }

    /**
     * 查询领域
     * @return
     */
    @GetMapping("/getTerritory")
    public AjaxResult getTerritory(Territory territory){
        List<Territory> territoryList = territoryService.getTerritory(territory);
        return AjaxResult.success(territoryList);
    }

    /**
     * 逻辑删除
     * @param territoryId
     * @return
     */
    @DeleteMapping("/delete/{territoryId}")
    public AjaxResult deleteTerritory(@PathVariable("territoryId") String[] territoryId){
        return toAjax(territoryService.deleteTerritory(territoryId));
    }

}
